generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-arm64-openssl-3.0.x"]
}

datasource db {
  provider = "sqlite"
  url      = env("SQLITE_DATABASE_URL")
}

model Account {
  id                String   @id @default(cuid())
  userId            String   @map("user_id")
  type              String?
  provider          String
  providerAccountId String   @map("provider_account_id")
  token_type        String?
  refresh_token     String? 
  access_token      String? 
  expires_at        Int?
  scope             String?
  id_token          String? 
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  user              User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
  @@map("accounts")
}

model Session {
  id           String   @id @default(cuid())
  userId       String?  @map("user_id")
  sessionToken String   @unique @map("session_token")
  accessToken  String?  @map("access_token")
  expires      DateTime
  user         User?    @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  @@map("sessions")
}

model User {
  id            String               @id @default(uuid())
  name          String?
  email         String               @unique
  emailVerified DateTime?            @map("email_verified")
  password      String?
  image         String?
  createdAt     DateTime             @default(now())
  updatedAt     DateTime             @updatedAt
  accounts      Account[]
  sessions      Session[]
  dbConfigs     DatabaseConfigUser[]
  createdDBConfigs DatabaseConfig[]

  @@map("users")
}

model VerificationRequest {
  id         String   @id @default(cuid())
  identifier String
  token      String   @unique
  expires    DateTime
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

  @@unique([identifier, token])
  @@map("verificationrequest")
}

// Database Config
model DatabaseConfig {
  id              String                @id @default(cuid())
  name            String
  environment     String                @default("production")
  username        String                @default("admin")             
	password        String?    
	host            String                @default("127.0.0.1")
	port            String                @default("8123")
	database        String                @default("doku")
	query           String?
  dbUsers         DatabaseConfigUser[]
  createdAt       DateTime              @default(now())
  updatedAt       DateTime              @updatedAt
  createdByUserId String                @map("user_id")
  createdByUser   User                  @relation(fields: [createdByUserId], references: [id])

  @@unique([name])
  @@map("databaseconfig")
}

model DatabaseConfigUser {
  databaseConfigId String
  userId           String          @map("user_id")
  user             User            @relation(fields: [userId], references: [id])
  databaseConfig   DatabaseConfig? @relation(fields: [databaseConfigId], references: [id])
  isCurrent        Boolean         @default(false)

  @@unique([databaseConfigId, userId])
  @@map("databaseconfiguser")
}
